package io.renren.modules.weixin.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import io.renren.modules.weixin.entity.ZhoupuEntity;
import io.renren.modules.weixin.service.ZhoupuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.weixin.entity.CaipuEntity;
import io.renren.modules.weixin.service.CaipuService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;


/**
 * 菜谱
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2019-02-02 09:01:33
 */
@RestController
@RequestMapping("weixin/caipu")
@Api("微信小程序-菜谱接口")
public class CaipuController {
    @Autowired
    private CaipuService caipuService;

    @Autowired
    private ZhoupuService zhoupuService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation("列表")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = caipuService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation("信息")
    public R info(@PathVariable("id") Integer id) {
        CaipuEntity caipu = caipuService.selectById(id);
        return R.ok().put("caipu", caipu);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation("保存")
    public R save(@RequestBody CaipuEntity caipu) {
        caipuService.insert(caipu);
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation("更新")
    public R update(@RequestBody CaipuEntity caipu) {
        caipuService.updateById(caipu);

        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @ApiOperation("删除")
    public R delete(@RequestBody Integer[] ids) {
        caipuService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 按日期查询菜谱
     */
    @GetMapping("/listbydate")
    @ApiOperation("按日期查询菜谱")
    public R selectByDate(@RequestParam("date") String date) {
        EntityWrapper<ZhoupuEntity> queryWrapper = new EntityWrapper<>();
        queryWrapper.eq("date", date);
        ZhoupuEntity zhoupu = zhoupuService.selectOne(queryWrapper);
        R ret = R.ok();
        String[] sd = zhoupu.getList().split("\\|");
        for (int i = 0; i < sd.length; i++) {
            System.out.println(sd[i]);
            String[] li = sd[i].split(",");
            List<Integer> idsList = new ArrayList<>();
            for (String str : li) {
                idsList.add(Integer.parseInt(str));
            }
            List<CaipuEntity> caipus = caipuService.selectBatchIds(idsList);
            ret.put("sd" + Integer.toString(i), caipus);
        }
        return ret;
    }

}
